package com.zkx.blogweb.controller;

import com.github.pagehelper.PageInfo;
import com.zkx.blogweb.entity.IdArticle;
import com.zkx.blogweb.entity.User;
import com.zkx.blogweb.mapper.service.BlogServiceImp;
import com.zkx.blogweb.mapper.service.IdArticleServiceImp;
import com.zkx.blogweb.mapper.service.UserMapperImp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zkx
 */
@Slf4j
@Controller
public class SelfController {

    @Autowired
    IdArticleServiceImp idArticleServiceImp;

    @Autowired
    BlogServiceImp blogServiceImp;

    @Autowired
    UserMapperImp userMapperImp;

    @GetMapping("/self")
    public String test(HttpSession session,Model model){
        User user = (User)session.getAttribute("user");
        Integer userId = user.getUserId();
        //获取这个人的所有博文数
        Integer blogs = idArticleServiceImp.selectBlogsById(userId);
        model.addAttribute("blogs",blogs);
        //总赞数
        Integer stars = idArticleServiceImp.selectStarsById(userId);
        model.addAttribute("stars",stars);
        String userName = user.getUserName();
        model.addAttribute("userName",userName);
        return "self";
    }
    /**
     * 前端发送ajax请求，刷新页面
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selfpage/{id}",method = {RequestMethod.POST,RequestMethod.GET})
    public PageInfo<IdArticle> getPage(@PathVariable("id") Integer id, HttpSession session,Model model){
        //从session中取出用户id,根据用户id查询文章
        User user = (User)session.getAttribute("user");
        Integer userId = user.getUserId();
        PageInfo<IdArticle> pageInfo = idArticleServiceImp.getPageInfoByBlogId(userId,id, 10);
        return pageInfo;
    }

    @RequestMapping(value = "/myBlog",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public String myBlogDesc(HttpSession session){
        User user = (User)session.getAttribute("user");
        Integer userBlog = user.getUserBlog();
        return blogServiceImp.myBlog(userBlog);
    }

    @RequestMapping(value = "/selfInfo",method = {RequestMethod.POST,RequestMethod.GET})
    public String selfInfoChange(HttpSession session,Model model){
        User user = (User)session.getAttribute("user");
        Date time = user.getCreateTime();
        DateFormat dFormat12 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); //hh表示12小时制；
        String createTime = dFormat12.format(time);
        model.addAttribute("createTime",createTime);
        return "self_info";
    }

    /**
     *
     * @param option
     * @param info 前端传来的修改数据
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/operation",method = {RequestMethod.POST})
    public String changeInfo(@RequestParam("option")String option,String info,HttpSession session){
        //先得到session里面·1的信息，再修改，给session
        User user = (User)session.getAttribute("user");
        if(option==null){
            return "修改失败";
        }else{
            if(option.equals("name")){
                //修改姓名
                user.setUserName(info);
            }else if(option.equals("password")){
                user.setUserPassword(info);
            }else if(option.equals("gender")){
                user.setUserGender(info);
            }else{
                return "修改失败";
            }
            userMapperImp.updateInfo(user);
            //更新session
            session.setAttribute("user",user);
            //数据库更新

        }
        System.out.println(user);

        return "修改成功";
    }
}
